package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketSystem;
import com.cskaoyan.market.db.domain.MarketSystemExample;
import com.cskaoyan.market.db.mapper.MarketSystemMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.cskaoyan.market.vo.MarketConfigVo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class MarketSystemServiceImpl implements MarketSystemService {
    @Override
    public MarketConfigVo read() {
        SqlSession marketSession = MybatisUtils.getSession();
        MarketConfigVo marketConfigVo=new MarketConfigVo();
        MarketSystemMapper marketSystemMapper = marketSession.getMapper(MarketSystemMapper.class);
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(new MarketSystemExample());
        marketSession.commit();
        marketSession.close();
        /*    String market_mall_address;
    String market_mall_latitude;
    String market_mall_longitude;
    String market_mall_name;
    String market_mall_phone;
    String market_mall_qq;*/
        for (MarketSystem marketSystem : marketSystems) {
            //TODO 利用反射实现，加入try-catch
            if("market_mall_address".equals(marketSystem.getKeyName())) {
                marketConfigVo.setMarket_mall_address(marketSystem.getKeyValue());
            }if("market_mall_latitude".equals(marketSystem.getKeyName())){
                marketConfigVo.setMarket_mall_latitude(marketSystem.getKeyValue());
            }if("market_mall_longitude".equals(marketSystem.getKeyName()))
                marketConfigVo.setMarket_mall_longitude(marketSystem.getKeyValue());
            if("market_mall_name".equals(marketSystem.getKeyName())){
                marketConfigVo.setMarket_mall_name(marketSystem.getKeyValue());
            }if("market_mall_phone".equals(marketSystem.getKeyName())){
                marketConfigVo.setMarket_mall_phone(marketSystem.getKeyValue());
            }if("market_mall_qq".equals(marketSystem.getKeyName())){
                marketConfigVo.setMarket_mall_qq(marketSystem.getKeyValue());
            }
        }

        return marketConfigVo;
    }

    @Override
    public MarketConfigVo update(MarketConfigVo marketConfigInput) {
        SqlSession marketSession = MybatisUtils.getSession();
        MarketConfigVo marketConfigVo=new MarketConfigVo();
        MarketSystemMapper marketSystemMapper = marketSession.getMapper(MarketSystemMapper.class);
        List<MarketSystem> marketSystems = marketSystemMapper.selectByExample(new MarketSystemExample());
        for (MarketSystem marketSystem : marketSystems) {
            if("market_mall_address".equals(marketSystem.getKeyName())) {
                marketSystem.setKeyValue(marketConfigInput.getMarket_mall_address());
            }if("market_mall_latitude".equals(marketSystem.getKeyName())){
                marketSystem.setKeyValue(marketConfigInput.getMarket_mall_latitude());
            }if("market_mall_longitude".equals(marketSystem.getKeyName()))
                marketConfigVo.setMarket_mall_longitude(marketConfigInput.getMarket_mall_longitude());
            if("market_mall_name".equals(marketSystem.getKeyName())){
                marketConfigVo.setMarket_mall_name(marketConfigInput.getMarket_mall_name());
            }if("market_mall_phone".equals(marketSystem.getKeyName())){
                marketConfigVo.setMarket_mall_phone(marketConfigInput.getMarket_mall_phone());
            }if("market_mall_qq".equals(marketSystem.getKeyName())){
                marketConfigVo.setMarket_mall_qq(marketConfigInput.getMarket_mall_qq());
            }
        }
       return this.read();
    }
}
